do "...\First.do"


********************************************************************************
* Merge PCP information
********************************************************************************

clear
use "$work\Yder_9518_wide.dta"

** Merge with background characteristics
merge m:1 pnr using "$work\children1.dta"
drop if _merge==2
drop _merge

** Education
merge m:1 pnr using "$work\children3.dta", force
drop if _merge==2
drop _merge 

* Ethnicity
merge m:1 pnr year using "$work\ie_type.dta"
drop if _merge==2
drop _merge

* Mothers education
merge m:1 mor_id using "$work\mom_udd.dta"
drop if _merge==2
drop _merge

* Fathers education
merge m:1 far_id using "$work\dad_udd.dta"
drop if _merge==2
drop _merge


********************************************************************************
* Defining Low SES PCPs
********************************************************************************

* Low ses doctors
g mom_ses=(mor_udd_level==10)
replace mom_ses=. if mor_udd_level==.

g dad_ses=(far_udd_level==10)
replace dad_ses=. if far_udd_level==.

g ses_parent = mom_ses
replace ses_parent=dad_ses if mom_ses==0 & dad_ses!=.

g mis_ses_parent=(ses_parent==.)
bys ydernr year: egen yder_ses_missing=mean(mis_ses_parent)
replace ses_parent=0 if ses_parent==.

bys ydernr year: egen yder_ses_max=max(ses_parent)
bys ydernr year: egen yder_ses_min=min(ses_parent)
bys ydernr year: egen yder_ses_mean=mean(ses_parent)

tab yder_ses_max 
tab yder_ses_min 
sum yder_ses_mean 

*** Higher levels of education - used in the external validation
*** do file: 7b_general_concordance.do

g p_20=(mor_udd_level==20 | far_udd_level==20) // High school
g p_35=(mor_udd_level==35 | far_udd_level==35) // Vocational education
g p_40=(mor_udd_level==40 | far_udd_level==40) // Associate degree
g p_50=(mor_udd_level==50 | far_udd_level==50) // Undergraduate degree 
g p_60=(mor_udd_level==60 | far_udd_level==60) // Postgraduate degree


foreach val of numlist 20 35 40 50 60 {
	
bys ydernr year: egen max_p_`val'=max(p_`val')

}



*********************************************************************************
** Medical school 
*********************************************************************************
merge m:1 pnr using "$work\doctor_udd.dta", keepus(gp_school year_doctor)
drop if _merge==2
drop _merge

tab gp_school, g(help)

bys ydernr year: egen ku=max(help1)
bys ydernr year: egen au=max(help2)
bys ydernr year: egen sdu=max(help3)
bys ydernr year: egen other=max(help4)

bys ydernr year: egen yder_ku=mean(help1)
bys ydernr year: egen yder_au=mean(help2)
bys ydernr year: egen yder_sdu=mean(help3)
bys ydernr year: egen yder_other=mean(help4)
drop help1-help4

*********************************************************************************
** Age
g age=year-yob
bys ydernr year: egen mean_age=mean(age)
replace mean_age=round(mean_age)

** Ethnicicty
g dk=(ie_type==1)
bys ydernr year: egen mean_dk=mean(dk)

** Gender
g male_doctor=(koen==1)
tab male
drop koen

bys ydernr year: egen mean_male=mean(male_doctor)

* grades 
merge m:1 pnr using "$work\gp_grades.dta"
drop if _merge==2
tab _merge 
drop _merge

g mis_grades=(karakter_udd==.)
bys ydernr year: egen mean_grades=mean(karakter_udd)
bys ydernr year: egen mean_grades_mis=mean(mis_grades)

bys yob: egen grades_std=std(karakter_udd)
bys ydernr year: egen mean_grades_std=mean(grades_std)


sum mean_grades, d 
sum mean_grades_mis, d 

replace solo=1 if N_doctors==1
replace solo=0 if N_doctors>1
tab solo
tab year solo, row

save "$work\Yder_9518_long.dta", replace



keep ydernr year mean_age mean_male mean_dk ///
 yder_ses_max yder_ses_min yder_ses_mean yder_ses_missing ///
 max_p_20 max_p_35 max_p_40 max_p_50 max_p_60 ///
 N_doctors  ku au sdu other yder_ku yder_au yder_sdu yder_other solo mean_grades
duplicates drop

save "$work\gp_ses.dta", replace

